#import "ElecMobileNativePlugin.h"
#import "GlobalData.h"
#import "Util.h"

@implementation ElecMobileNativePlugin {
    GlobalData *global;
    CLLocationManager *locationManager;
}

+ (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
    NSObject <FlutterBinaryMessenger> *messenger = [registrar messenger];
    ElecMobileNativePlugin *instance = [[ElecMobileNativePlugin alloc] init];

    FlutterMethodChannel *methodChannel = [FlutterMethodChannel methodChannelWithName:method_channel_name binaryMessenger:messenger];
    [registrar addMethodCallDelegate:instance channel:methodChannel];
}

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
    NSLog(@"flutter call native\n-------method: %@\n----arguments: %@", call.method, call.arguments);

    if (!global) [self initGlobalData];

    id res = nil;
    BOOL callBackNow = YES;

        /** 基础功能 */
    if ([fcn_sysVer isEqualToString:call.method]) {
        res = [Util getSystemVersion];
    } else if ([fcn_appVer isEqualToString:call.method]) {
        res = [Util getAppVersion];
    } else if ([fcn_imei isEqualToString:call.method]) {
        res = [Util getIDFA];
    } else if ([fcn_show_sysOption isEqualToString:call.method]) {
        [Util showSysOption];
    } else if ([fcn_show_sysTel isEqualToString:call.method]) {
        [Util showSysTel:call.arguments];
    } else if ([fcn_wifi_enabled isEqualToString:call.method]) {
        res = @(NO);
    } else if ([fcn_wifi_current isEqualToString:call.method]) {
        res = [self getWifiName];
    } else if ([fcn_initBugly isEqualToString:call.method]) {
        [Util initBugly];
    } else if ([fcn_toast isEqualToString:call.method]) {
        [Util toast:call.arguments];
    } else if ([fcn_checkUpgrade isEqualToString:call.method]) {
        //do nothing
    } else if ([fcn_get_file_length isEqualToString:call.method]) {
        res = @([global.fileSelector getFileLength:call.arguments]);
    } else if ([fcn_wifi_strength isEqualToString:call.method]) {
        res = @(-1);
    } else if ([fcn_ios_request_location_auth isEqualToString:call.method]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:MSG_appDelegate_request_location_auth object:nil];
    }

        /** 本地存储相关 */
    else if ([fcn_localStorage_insert_string isEqualToString:call.method]) {
        [Util localStorage_insert_string:call.arguments];
    } else if ([fcn_localStorage_insert_int isEqualToString:call.method]) {
        [Util localStorage_insert_int:call.arguments];
    } else if ([fcn_localStorage_delete isEqualToString:call.method]) {
        [Util localStorage_delete:call.arguments];
    } else if ([fcn_localStorage_update isEqualToString:call.method]) {
        [Util localStorage_update:call.arguments];
    } else if ([fcn_localStorage_select_string isEqualToString:call.method]) {
        res = [Util localStorage_select_string:call.arguments];
    } else if ([fcn_localStorage_select_int isEqualToString:call.method]) {
        res = @([Util localStorage_select_int:call.arguments]);
    }

        /** 相册素材选择相关 */
    else if ([fcn_selectPhoto isEqualToString:call.method]) {
        [global.fileSelector setType:ImagePickerType_photo result:result];
        [global.fileSelector selectMulti];
        callBackNow = NO;
    } else if ([fcn_selectVideo isEqualToString:call.method]) {
        [global.fileSelector setType:ImagePickerType_video result:result];
        [global.fileSelector selectMulti];
        callBackNow = NO;
    }

        /** 计划数据库相关 */
    else if ([fcn_schedule_create isEqualToString:call.method]) {
        res = [ElecSchedule create:call.arguments];
    } else if ([fcn_schedule_delete isEqualToString:call.method]) {
        [ElecSchedule delete:call.arguments];
    } else if ([fcn_schedule_update isEqualToString:call.method]) {
        [ElecSchedule update:call.arguments];
    } else if ([fcn_schedule_select isEqualToString:call.method]) {
        res = [ElecSchedule select:call.arguments];
    } else if ([fcn_schedule_select_page isEqualToString:call.method]) {
        res = [ElecSchedule select_page:call.arguments];
    } else if ([fcn_schedule_check_expired isEqualToString:call.method]) {
        res = [ElecSchedule check_expired:call.arguments];
    } else if ([fcn_schedule_clear isEqualToString:call.method]) {
        [ElecSchedule clear:call.arguments];
    }

        /** 素材数据库相关 */
    else if ([fcn_material_insert isEqualToString:call.method]) {
        res = [ElecMaterial insert:call.arguments];
    } else if ([fcn_material_delete isEqualToString:call.method]) {
        [ElecMaterial delete:call.arguments];
    } else if ([fcn_material_update isEqualToString:call.method]) {
        res = [ElecMaterial update:call.arguments];
    } else if ([fcn_material_order isEqualToString:call.method]) {
        [ElecMaterial order:call.arguments];
    } else if ([fcn_material_select isEqualToString:call.method]) {
        res = [ElecMaterial select:call.arguments];
    }

        /** 缓存文件相关 */
    else if([fcn_get_cache_file isEqualToString:call.method]){
        res = global.fileSelector.getNewUpgradeFileCachePath;
    } else if([fcn_delete_cache_file isEqualToString:call.method]){
        [global.fileSelector deleteFile:call.arguments];
    }

        /** 设备相关 */
    else if([fcn_device_store isEqualToString:call.method]){
        res = @([ElecDevice store:call.arguments]);
    } else if([fcn_device_delete isEqualToString:call.method]){
        res = @([ElecDevice delete:call.arguments]);
    } else if([fcn_device_clear isEqualToString:call.method]){
        [ElecDevice clear:call.arguments];
    } else if([fcn_device_query isEqualToString:call.method]){
        res = [ElecDevice query:call.arguments];
    }

        /** 设备升级相关 */
    else if([fcn_upgrade_create isEqualToString:call.method]){
        res = [ElecUpgrade create:call.arguments];
    } else if([fcn_upgrade_clear isEqualToString:call.method]){
        res = @([ElecUpgrade clear:call.arguments]);
    } else if([fcn_upgrade_delete isEqualToString:call.method]){
        res = @([ElecUpgrade delete:call.arguments]);
    } else if([fcn_upgrade_download isEqualToString:call.method]){
        res = [ElecUpgrade download:call.arguments];
    } else if([fcn_upgrade_update isEqualToString:call.method]) {
        res = [ElecUpgrade update:call.arguments];
    }

        /** 设备升级联查相关 */
    else if([fcn_device_upgrade_query isEqualToString:call.method]) {
        res = [ElecUpgrade device_upgrade_query:call.arguments];
    }

        /** 未定义接口 返回异常 */
    else {
        res = FlutterMethodNotImplemented;
    }

    if (callBackNow) {
        [self callBackResult:res result:result method:call.method];
    }
}

- (void)callBackResult:(nullable id)data result:(FlutterResult)res method:(NSString *)method {
    if ([Util checkDefined:data]) {
        NSLog(@"------%@  res:%@", method, data);
        res(data);
    } else {
        res(nil);
    }
}

- (NSString *)getWifiName {
    return [Util wifiName];
}

- (void)initGlobalData {
    global = [GlobalData shareInstance];
    [global.fileSelector deleteAllUnusedFile];
    [global.fileSelector deleteAllUpgradeCache];
}

@end
